#include "managerdao_file_impl.h"
#include "tools.h"
#include <unistd.h>
#include <vector>
using namespace std;
using namespace MAN;

ManagerDaoFileImpl::ManagerDaoFileImpl()
{

}

//从文件读取管理员信息。以二进制方式整块读取Manager对象，加入从参数传入的管理员容器 
void ManagerDaoFileImpl::load(vector<Manager>& vec)
{
	FILE* frp= fopen("account/managers.dat","rb");
	if(frp == NULL)
	{
		return ;
	}
	Manager man_tmp;
	while(1)
	{	
		int ret = fread(&man_tmp,sizeof(Manager),1,frp);

		if(ret <= 0)
		{
			break;
		}
		vec.push_back(man_tmp);		
	}
	return ;

}

//将管理员信息写入文件。遍历从参数传入的管理员容器，以二进制方式整块写入每一个Manager对象
void ManagerDaoFileImpl::save(vector<Manager>& vec)
{
	FILE* frp= fopen("account/managers.dat","w");
	if(frp == NULL)
	{
		perror("open");
		return ;
	}

	for(unsigned int i =0; i < vec.size(); i++)
	{
		fwrite(&vec[i],sizeof(Manager),1,frp);
	}
	/*Manager tmp;
	while(1)
	{
		if(vec.empty()) break;
		tmp = vec.back();
		fwrite(&tmp,sizeof(Manager),1,frp);
		vec.pop_back();	
	}
	
	*/
}

ManagerDaoFileImpl::~ManagerDaoFileImpl()
{
	
}
	
